import {createSlice} from "@reduxjs/toolkit";

// 默认用户
const DEFAULT_USER = {
  id: -1,
  address: "",
  avatarUrl: "/assets/loginUser.png",
  createTime: "",
  email: "",
  gender: -1,
  nickName: "",
  updateTime: "",
  userProfile: "暂无简介",
  userRole: "notLogin",
  phone: "",
  wxcode: "",
  username: "",
};

/**
 * 登录用户全局状态
 */
export const loginUserSlice = createSlice({
  name: "loginUser",
  initialState: DEFAULT_USER,
  reducers: {
    setLoginUser: (state, action) => {
      return {
        ...state,
        ...action.payload
      };
    },
    updateUserInfo: (state, action) => {
      const updatedUser = {
        ...state,
        ...action.payload
      };
      Object.assign(DEFAULT_USER, updatedUser);
      return updatedUser;
    },
    logout: () => DEFAULT_USER,
  },
});

export { DEFAULT_USER };

// 修改状态
export const { setLoginUser, updateUserInfo, logout } = loginUserSlice.actions;

export default loginUserSlice.reducer;
